package com.untour.im.client.command;

import cn.hutool.core.util.RandomUtil;
import com.untour.im.client.message.ClientMessage;
import com.untour.im.codec.protocol.command.Command;
import com.untour.im.codec.protocol.message.body.HeartBeatMessageBody;
import com.untour.im.codec.protocol.message.body.ImMessageBody;
import com.untour.im.codec.protocol.message.content.LoginRequestMessageContent;
import com.untour.im.codec.protocol.message.content.SingleRequestMessageContent;
import com.untour.im.codec.protocol.message.content.TxtMessageContent;
import com.untour.im.codec.protocol.serialize.Serializer;
import com.untour.im.codec.protocol.serialize.SerializerAlgorithm;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

/**
 * @description: 将ByteBuf转化为Message实体，根据私有协议转换
 * 私有协议规则，
 * 4位表示Command表示消息的开始，
 * 4位表示version
 * 4位表示clientType
 * 4位表示messageType
 * 4位表示appId
 * 4位表示imei长度
 * 4位表示数据长度
 * imei
 * data
 */
public class ClientPacketEncodec {
    public static final ClientPacketEncodec INSTANCE = new ClientPacketEncodec();

    private static final Byte messageTYpe = SerializerAlgorithm.JSON;
    private static final String imei = "12313";
    private static final Integer appId = 1;
    private static final Integer version = 1;



    public void sendHeartBeatRequestMessage(ChannelHandlerContext ctx) {
        ClientMessage requestPacket = new ClientMessage(Command.HEARTBEAT_REQUEST,
                new HeartBeatMessageBody(RandomUtil.randomNumbers(20),
                new TxtMessageContent("我还在呢，别关闭我！")));
        ctx.channel().writeAndFlush(requestPacket);
    }

    public void sendSingleToUserRequestMessage(Channel channel, String toUserId, String message) {
        ClientMessage requestPacket = new ClientMessage(Command.SINGLE_MESSAGE_REQUEST,
                new ImMessageBody<>(RandomUtil.randomNumbers(20),
                        new SingleRequestMessageContent(new TxtMessageContent(message),
                                toUserId)));
        System.out.println("发送消息给用户：" + toUserId + "," + message);
        channel.writeAndFlush(requestPacket);
    }

    public void sendLoginRequestMessage(Channel channel, String token, String message) {
        ClientMessage requestPacket = new ClientMessage(Command.LOGIN,
                new ImMessageBody<>(RandomUtil.randomNumbers(20),
                        new LoginRequestMessageContent(new TxtMessageContent(message),
                                token)));
        System.out.println("用户：" + token + " 登录," + message);
        channel.writeAndFlush(requestPacket);
    }


    public void encode(ByteBuf byteBuf, ClientMessage requestPacket) {
        Serializer serializer = SerializerCodec.getSerializer(messageTYpe);

        // 1. 序列化 java 对象
        byte[] bytes = serializer.serialize(requestPacket.getMessageBody());

        // 2. 实际编码过程
        byteBuf.writeByte(requestPacket.getCommand());
        byteBuf.writeInt(version);
        byteBuf.writeInt(requestPacket.getClientType());
        byteBuf.writeByte(messageTYpe);
        byteBuf.writeInt(appId);
        byteBuf.writeInt(imei.length());
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(imei.getBytes());
        byteBuf.writeBytes(bytes);
    }
}
